from PyQt5.QtGui import QIcon, QPixmap
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QPushButton, QLabel, QLineEdit, QHBoxLayout, QMessageBox, QFrame, \
    QRadioButton
from PyQt5.QtCore import Qt
from config import button_config,Text_config,input_config,frame_config
import requests
from menu_client import Menu1
from admin_menu import admin_menu

class Login(QWidget):
    def __init__(self,parent=None):
        super().__init__()
        self.parent=parent
        self.client=self.parent.client
        self.setWindowTitle("登录")
        icon=QIcon('../static/img/商城图标.png')
        self.setWindowIcon(icon)
        self.resize(400, 600)
        main_layout = QVBoxLayout(self)
        self.initUI(main_layout)
    def initUI(self,layout):
        logo = QLabel(self)
        img=QPixmap('../static/img/商城图标.png')
        img=img.scaled(200, 200, Qt.KeepAspectRatio)
        logo.setPixmap(img)
        logo.setAlignment(Qt.AlignCenter)
        layout.addWidget(logo)

        text1=QLabel('用户登录')
        text1.setStyleSheet(Text_config.text_style1)
        text1.setAlignment(Qt.AlignCenter)
        layout.addWidget(text1)


        frame=QFrame()
        frame.setStyleSheet(frame_config.frame_style1)
        layout.addWidget(frame)
        layout1=QVBoxLayout(frame)
        num_text=QLabel('账号')
        num_text.setStyleSheet(Text_config.text_style2)
        num_text.setMinimumHeight(50)
        layout1.addWidget(num_text)
        self.num_input=QLineEdit()
        self.num_input.setMinimumHeight(50)
        self.num_input.setStyleSheet(input_config.input_style2)
        self.num_input.setPlaceholderText('请输入账号')
        layout1.addWidget(self.num_input)

        pwd_text=QLabel('密码')
        pwd_text.setMinimumHeight(50)
        pwd_text.setStyleSheet(Text_config.text_style2)
        layout1.addWidget(pwd_text)
        self.pwd_input=QLineEdit()
        self.pwd_input.setMinimumHeight(50)
        self.pwd_input.setStyleSheet(input_config.input_style2)
        self.pwd_input.setPlaceholderText('请输入密码')
        self.pwd_input.setEchoMode(QLineEdit.Password)
        layout1.addWidget(self.pwd_input)

        layout3=QHBoxLayout()
        layout1.addLayout(layout3)
        self.button1=QRadioButton("🛒用户")
        self.button1.setStyleSheet("""
            padding-left: 80px;
        """)
        self.button2=QRadioButton("👨管理者")
        layout3.addWidget(self.button1)
        layout3.addWidget(self.button2)

        button_layout=QHBoxLayout()
        submit_button=QPushButton('提交')
        exit_button=QPushButton('取消')
        submit_button.setMinimumHeight(50)
        exit_button.setMinimumHeight(50)
        submit_button.setStyleSheet(button_config.button_style2)
        exit_button.setStyleSheet(button_config.button_style3)
        button_layout.addWidget(submit_button)
        button_layout.addWidget(exit_button)
        layout.addLayout(button_layout)

        exit_button.clicked.connect(self.exit)
        submit_button.clicked.connect(self.submit)
    def exit(self):
        if self.parent:
            self.parent.show()
        self.close()

    def submit(self):
        num=self.num_input.text()
        pwd=self.pwd_input.text()
        if not num or not pwd:
            QMessageBox.critical(None,'错误','请将数据输入完整')
            return
        if self.button1.isChecked():
            is_admin=False
        elif self.button2.isChecked():
            is_admin=True
        else:
            QMessageBox.critical(None, '错误', '请选择身份')
            return
        parser = {
            'username': num,
            'password': pwd,
            'is_admin': is_admin
        }

        statu=self.client.login(parser)
        if statu=='登录成功':
            if is_admin:
                self.window=admin_menu(self)
                self.window.show()
                self.close()
            else:
                self.window=Menu1(self)
                self.window.show()
                self.close()
        else:
            QMessageBox.critical(None, '错误', statu)

    def closeEvent(self,event):
        pass